//
//  TTDetailSimilarityViewCell.m
//  TaoTuo
//
//  Created by LG on 2020/11/16.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTDetailSimilarityViewCell.h"

@interface TTDetailSimilarityViewCell ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImageView *selloutImageView;    // 售罄
@property (nonatomic, strong) UILabel *titleLabel;  // 标题
@property (nonatomic, strong) UILabel *priceLabel;  // 价格
@property (nonatomic, strong) UILabel *kucunLabel;  // 库存

@end


@implementation TTDetailSimilarityViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setupSubviews];
        
    }
    return self;
}

- (void)setModel:(TTHomeListModel *)model
{
    _model = model;
    
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.GoodsImage] placeholderImage:[UIImage imageNamed:@"f_placeholder_img"] options:SDWebImageRetryFailed];
    
    self.selloutImageView.hidden = (model.StockCount.intValue > 0 || model.CustomizedCount > 0);
    
    self.titleLabel.text = model.GoodsTitle;
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"￥%@", model.Price]];
    [attrStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:10] range:NSMakeRange(0, 1)];
    self.priceLabel.attributedText = attrStr;
    if (model.StockCount.intValue == 0) {
        self.kucunLabel.text = [NSString stringWithFormat:@"定制库存:%ld件", (long)model.CustomizedCount];
    } else {
        self.kucunLabel.text = [NSString stringWithFormat:@"库存:%@件", model.StockCount];
    }

    
}

- (void)setupSubviews
{
    self.bgView.frame = CGRectMake(0, 0, self.width, 105);
    self.imageView.frame = CGRectMake(0, 0, self.width - 32, self.bgView.height - 15);
    self.imageView.center = self.bgView.center;
    self.selloutImageView.frame = self.imageView.bounds;
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_bottom).mas_offset(9);
        make.left.mas_equalTo(0);
        make.right.equalTo(self.contentView.mas_right).mas_offset(-5);
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(9);
        make.left.equalTo(self.titleLabel);
    }];
    
    [self.kucunLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.priceLabel);
        make.right.equalTo(self.contentView.mas_right).mas_offset(-5);
    }];
    
}

- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 5;
        _bgView.layer.borderWidth = 0.5;
        _bgView.layer.borderColor = UIColorHex(#E6E6E6).CGColor;
        [self.contentView addSubview:_bgView];
    }
    return _bgView;
}

- (UIImageView *)imageView
{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        [self.bgView addSubview:_imageView];
        
    }
    return _imageView;
}

- (UIImageView *)selloutImageView
{
    if (!_selloutImageView) {
        _selloutImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"homelistsellout"]];
        _selloutImageView.contentMode = UIViewContentModeCenter;
        [self.bgView addSubview:_selloutImageView];
    }
    return _selloutImageView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel labelWithTitle:@"" color:[UIColor blackColor] fontSize:13];
        [self.bgView addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UILabel *)priceLabel
{
    if (!_priceLabel) {
        _priceLabel = [UILabel labelWithTitle:@"￥0.00" color:UIColorHex(FF0000) font:[UIFont boldSystemFontOfSize:14]];
        [self.bgView addSubview:_priceLabel];
    }
    return _priceLabel;
}

- (UILabel *)kucunLabel
{
    if (!_kucunLabel) {
        _kucunLabel = [UILabel labelWithTitle:@"" color:UIColorHex(999999) fontSize:8];
        [self.bgView addSubview:_kucunLabel];
    }
    return _kucunLabel;
}




@end
